package com.ms.rc_lab;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import androidx.appcompat.app.ActionBar;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.ms.rc_lab.databinding.ActivityHomeBinding;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

public class HomeActivity extends AppCompatActivity {

    private ActivityHomeBinding binding;
    private View mActionBarView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
//        getSupportActionBar().hide();
//        getSupportActionBar().setSubtitle("test");
        ActionBar.LayoutParams lp =new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
        mActionBarView = LayoutInflater.from(this).inflate(R.layout.custom_actionbar, null);
        getSupportActionBar().setCustomView(mActionBarView);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        binding = ActivityHomeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dc, R.id.navigation_servo)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_home);
//        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(binding.navView, navController);

        TcpClient.getInstance().setListener(new TcpClient.TcpClientListener() {
            @Override
            public void onMessageReceived(String message) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ((TextView)mActionBarView.findViewById(R.id.statusView)).setText(message);
                        ((TextView)mActionBarView.findViewById(R.id.statusView)).setTextColor(Color.RED);
                    }
                });
            }

            @Override
            public void onConnectionStatusChanged(boolean isConnected) {
                if(isConnected){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ((TextView)mActionBarView.findViewById(R.id.statusView)).setText("(设备状态： 已连接)");
                            ((TextView)mActionBarView.findViewById(R.id.statusView)).setTextColor(Color.GREEN);
                        }
                    });

                }else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ((TextView)mActionBarView.findViewById(R.id.statusView)).setText("(设备状态： 连接断开)");
                            ((TextView)mActionBarView.findViewById(R.id.statusView)).setTextColor(Color.RED);
                        }
                    });
                }
            }

            @Override
            public void onConnectionFailed() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ((TextView)mActionBarView.findViewById(R.id.statusView)).setText("(设备状态： 连接失败)");
                        ((TextView)mActionBarView.findViewById(R.id.statusView)).setTextColor(Color.RED);
                    }
                });
            }
        });
        TcpClient.getInstance().start();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        TcpClient.getInstance().stop();
    }
}